define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exchange/ordergoods/index' + location.search,
                    add_url: 'exchange/ordergoods/add',
                    edit_url: 'exchange/ordergoods/edit',
                    del_url: 'exchange/ordergoods/del',
                    multi_url: 'exchange/ordergoods/multi',
                    import_url: 'exchange/ordergoods/import',
                    table: 'ordergoods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
//                      {field: 'agent_id', title: __('Agent_id')},
//                      {field: 'store_no', title: __('store_no')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'total_pay_price', title: __('Total_pay_price'), operate:'BETWEEN'},
                        {field: 'discount_price', title: __('Discount_price'), operate:'BETWEEN'},
                        {field: 'is_pay', title: __('付款状态'), searchList: {"0":__('待付款'),"2":__('已付款'),"1":__('已退款')}, formatter: Table.api.formatter.normal},
                        {field: 'pay_type', title: __('付款方式'), searchList: {"1":__('微信'),"2":__('支付宝'),"3":__('云闪付'),"4":__('pos机'),"5":__('现金')}, formatter: Table.api.formatter.normal},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('订单状态'), searchList: {"0":__('交易'),"2":__('退款')}, formatter: Table.api.formatter.status},
                        {field: 'source_type', title: __('终端'), searchList: {"1":__('小程序'),"2":__('PC')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [{
                                
					            name: 'ajax',
						            text: __('退款'),
						            title: __('退款'),
						            classname: 'btn btn-xs btn-success btn-magic btn-ajax',
						            icon: 'fa fa-magic',
						            url: 'exchange/ordergoods/show',
						            confirm: '确认退款',
						             visible: function (row) {
                                           // 自定义按钮 动态是否显示
                                           if (row.is_pay == 2) {
                                           	   if (row.draw == 1) {
	                                               return false;
	                                           }
                                               return true;
                                           }
                                           return false;
                                      },
						            success: function (data, ret) {
						                Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
						                //如果需要阻止成功提示，则必须使用return false;
						                //return false;
						            },
						            error: function (data, ret) {
						                console.log(data, ret);
						                Layer.alert(ret.msg);
						                return false;
						            }


                            },{
                                
					            name: 'ajax',
						            text: __('补打'),
						            title: __('补打'),
						            classname: 'btn btn-xs btn-success btn-magic btn-ajax',
						            icon: 'fa fa-magic',
						            url: 'exchange/ordergoods/print',
						            confirm: '确认补打小票吗',
						             visible: function (row) {
                                           // 自定义按钮 动态是否显示
                                            return true;
                                      },
						            success: function (data, ret) {
						                Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
						                //如果需要阻止成功提示，则必须使用return false;
						                //return false;
						            },
						            error: function (data, ret) {
						                console.log(data, ret);
						                Layer.alert(ret.msg);
						                return false;
						            }


                            },{
                                
					            name: 'ajax',
						            text: __('删除'),
						            title: __('删除'),
						            classname: 'btn btn-xs btn-success btn-magic btn-ajax',
						            icon: 'fa fa-magic',
						            url: 'exchange/ordergoods/deleteshow',
						            confirm: '确认删除',
						             visible: function (row) {
                                           // 自定义按钮 动态是否显示
                                           if (row.is_delete == 0) {
                                               return true;
                                           }
                                           return false;
                                      },
						            success: function (data, ret) {
						                Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
						                //如果需要阻止成功提示，则必须使用return false;
						                //return false;
						            },
						            error: function (data, ret) {
						                console.log(data, ret);
						                Layer.alert(ret.msg);
						                return false;
						            }


                            }],formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
